import React,{useState,useMemo} from 'react'

const Memo = ({num}) => {
    const[val, setVal] = useState('');
    const[sum, setSum] = useState(0);
    const cal =()=>{
        let number = 0;
        for(let i = 0; i <= val; i++){
            number = number + i;
        }
        setSum(number);
    }

    const count = useMemo(() => {
        console.log(123);
        let s = 0;
        for(let i = 0; i < val; i++){
            s += i
        }
        return s;
    }, [num])

    
    return (
        <div>
            <div>当前组件传递的结果-----{sum}</div>
            <div>父组件传递的结果----{count}</div>
            <input 
                type="text" 
                onChange={(e)=>setVal(e.target.value)}/>
            <button onClick={cal}>相加</button>
        </div>
    )
}

export default Memo
/**
 * 在App.js中 要设置初始状态num 传给count的函数
 * const[num, setSum] = useState(1000);
 */